using System;
using System.Text;

internal static class StringExtensions
{
	private static readonly char[] sCharsToMatch = new char[5]
	{
		'<',
		'>',
		'&',
		'\'',
		'"'
	};

	private static readonly string[] sStringsToReplaceWith = new string[5]
	{
		"&lt;",
		"&gt;",
		"&amp;",
		"&apos;",
		"&quot;"
	};

	internal static bool Contains(this string str, string subString, StringComparison compareType)
	{
		if (string.IsNullOrEmpty(str))
		{
			return false;
		}
		return str.IndexOf(subString, compareType) >= 0;
	}

	internal static string ReplaceXmlChars(this string str)
	{
		return Replace(str, sCharsToMatch, sStringsToReplaceWith);
	}

	internal static int CompareWithAmpersand(char[] strArray, int index, int length, int ampArrayIndex)
	{
		int length2 = sStringsToReplaceWith[ampArrayIndex].Length;
		if (length - index < length2)
		{
			return 0;
		}
		for (int i = index; i < index + length2; i++)
		{
			if (sStringsToReplaceWith[ampArrayIndex][i - index] != strArray[i])
			{
				return 0;
			}
		}
		return length2;
	}

	internal static int CompareWithAmpersand(char[] strArray, int index, int length, out int charsToSkip)
	{
		charsToSkip = 1;
		int num = 0;
		for (int i = 0; i < sStringsToReplaceWith.Length; i++)
		{
			if ((num = CompareWithAmpersand(strArray, index, length, i)) > 0)
			{
				charsToSkip = num;
				return i;
			}
		}
		return 0;
	}

	private static string Replace(string str, char[] charsToMatch, string[] stringsToReplaceWith)
	{
		if (string.IsNullOrEmpty(str))
		{
			return string.Empty;
		}
		int num = -1;
		if ((num = str.IndexOfAny(charsToMatch)) == -1)
		{
			return str;
		}
		StringBuilder stringBuilder = new StringBuilder(str.Length + 100);
		char[] array = str.ToCharArray();
		bool flag = false;
		if (num != 0)
		{
			stringBuilder.Append(array, 0, num);
		}
		while (num < array.Length)
		{
			flag = false;
			for (int i = 0; i < charsToMatch.Length; i++)
			{
				if (array[num] != charsToMatch[i])
				{
					continue;
				}
				if (array[num] == '&')
				{
					int charsToSkip = 1;
					int num2 = 0;
					if ((num2 = CompareWithAmpersand(array, num, str.Length, out charsToSkip)) == 0)
					{
						stringBuilder.Append(stringsToReplaceWith[i]);
					}
					else
					{
						stringBuilder.Append(stringsToReplaceWith[num2]);
					}
					num += charsToSkip;
					flag = true;
				}
				else
				{
					stringBuilder.Append(stringsToReplaceWith[i]);
					flag = true;
					num++;
				}
				break;
			}
			if (!flag)
			{
				stringBuilder.Append(array[num]);
				num++;
			}
		}
		return stringBuilder.ToString();
	}

	public static int IndexOfAny(char[] strArray, int offset, char[] charsToMatch, int inputLength = 0)
	{
		if (strArray == null || charsToMatch == null)
		{
			return -1;
		}
		if (inputLength == 0 || inputLength > strArray.Length)
		{
			inputLength = strArray.Length - offset;
		}
		for (int i = offset; i < inputLength; i++)
		{
			char c = strArray[i];
			for (int j = 0; j < charsToMatch.Length; j++)
			{
				if (charsToMatch[j] == c)
				{
					return i;
				}
			}
		}
		return -1;
	}

	public static int Replace(StringBuilder sb, char[] strArray, int offset, int arrayCount, char[] charsToMatch, string[] stringsToReplaceWith)
	{
		if (strArray == null || sb == null)
		{
			return 0;
		}
		if (charsToMatch == null)
		{
			charsToMatch = sCharsToMatch;
		}
		int length = sb.Length;
		if (stringsToReplaceWith == null)
		{
			stringsToReplaceWith = sStringsToReplaceWith;
		}
		arrayCount = ((arrayCount != 0) ? (arrayCount - offset) : (strArray.Length - offset));
		int num = offset + arrayCount;
		int num2 = -1;
		if ((num2 = IndexOfAny(strArray, offset, charsToMatch, num)) == -1)
		{
			sb.Append(strArray, offset, arrayCount);
			return arrayCount;
		}
		bool flag = false;
		if (num2 != 0)
		{
			if (num2 >= num - 1)
			{
				sb.Append(strArray, offset, arrayCount);
				return arrayCount;
			}
			sb.Append(strArray, offset, num2);
		}
		while (num2 < num)
		{
			flag = false;
			for (int i = 0; i < charsToMatch.Length; i++)
			{
				if (strArray[num2] != charsToMatch[i])
				{
					continue;
				}
				if (strArray[num2] == '&')
				{
					int charsToSkip = 1;
					int num3 = 0;
					if ((num3 = CompareWithAmpersand(strArray, num2, num, out charsToSkip)) == 0)
					{
						sb.Append(stringsToReplaceWith[i]);
					}
					else
					{
						sb.Append(stringsToReplaceWith[num3]);
					}
					num2 += charsToSkip;
					flag = true;
				}
				else
				{
					sb.Append(stringsToReplaceWith[i]);
					flag = true;
					num2++;
				}
				break;
			}
			if (!flag)
			{
				sb.Append(strArray[num2]);
				num2++;
			}
		}
		return sb.Length - length;
	}
}
